﻿Imports Ionic.Zip
Imports System.IO
Imports System.Collections
Imports System.Net
Imports tukui.updater.core


Class Main
    'Dim WoWpath As String
    'Dim over_config As Boolean
    'Dim mod_dl As String
    Private WithEvents BackWorker As New System.ComponentModel.BackgroundWorker

    Private Sub btn_settings_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_settings.Click
        My.Windows.options.Show()
    End Sub

    Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_update.Click
        BackWorker.WorkerReportsProgress = True
        BackWorker.RunWorkerAsync()
    End Sub



    Private Sub BackWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackWorker.ProgressChanged
        ' Update the progress bar
        Me.progress.Value = e.ProgressPercentage
        ' Update the progress textblock
        Me.txtProgress.Text = e.ProgressPercentage & "%"

        Select Case e.ProgressPercentage
            Case 0
                txtStatus.Text = "Copying Configs"
            Case 16
                txtStatus.Text = "Downloading files"
            Case 33
                txtStatus.Text = "Removing old installation"
            Case 50
                txtStatus.Text = "Processing Files"
            Case 100
                txtStatus.Text = "Finished"
            Case Else
                txtStatus.Text = ""
        End Select
    End Sub

    Private Sub BackWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackWorker.RunWorkerCompleted
        'To make sure it updates to "Finished"
        txtStatus.Text = "Finished"
        progress.Value = 100
        txtProgress.Text = "100%"
        MsgBox("Update finished!", MsgBoxStyle.OkOnly, "TukUI Updater finished!")
    End Sub

    Private Sub BackWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackWorker.DoWork
        Dim WoWpath As String = My.Settings.WoWPath & "\Interface\Addons"
        Dim Core As New core
        Dim Backup As Boolean
        BackWorker.ReportProgress(0)
        If My.Settings.over_config = False Then
            Core.Backup(WoWpath)
            Backup = True
        End If
        BackWorker.ReportProgress(16)
        Core.Download(My.Settings.mod_dl, WoWpath)
        BackWorker.ReportProgress(33)
        Core.Clean(WoWpath)
        BackWorker.ReportProgress(50)
        Core.ProcessFiles(WoWpath, Backup)
        BackWorker.ReportProgress(100)

    End Sub

    Public Sub onDragDelta(ByVal sender As Object, ByVal e As Primitives.DragDeltaEventArgs)
        Canvas.SetLeft(Me, Canvas.GetLeft(Me) + e.HorizontalChange)
        Canvas.SetTop(Me, Canvas.GetTop(Me) + e.VerticalChange)
    End Sub


    Private Sub Main_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
        'Populate Variables Phase
        'Progress Bar Initialization Sub Phase
        progress.Minimum = 0
        progress.Maximum = 100
        progress.Value = 0

        'Update Lables Phase
        'insert lable code here
    End Sub

    Private Sub btn_close_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_close.Click
        Me.Close()
    End Sub


    Private Sub btn_mini_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_mini.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub btn_visit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_visit.Click
        Dim webAddress As String = "http://www.tukui.org"

        Process.Start(webAddress)
    End Sub
End Class
